<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8'>
    <meta content='width=device-width, initial-scale=1.0' name='viewport'>
    <meta content='' name='description'>
    <meta content='Nils Nordman' name='author'>
    <link href='/images/howl.png' rel='shortcut icon'>
    <title>Howl :: Spec - howl.io.File</title>
    <link href="/stylesheets/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/syntax.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="/stylesheets/howl.css" media="screen" rel="stylesheet" type="text/css" />
    <script src="https://code.jquery.com/jquery-1.12.3.min.js" type="text/javascript"></script>
    <script src="/javascripts/bootstrap.min.js" type="text/javascript"></script>
    
    <link href='//fonts.googleapis.com/css?family=Josefin+Slab' rel='stylesheet' type='text/css'>
    <link href='//fonts.googleapis.com/css?family=Open+Sans+Condensed:700' rel='stylesheet' type='text/css'>
  </head>
  <body class='doc doc_spec doc_spec_io doc_spec_io_file_spec'>
    <div class='container'>
      <div class='masthead'>
        <ul class='nav nav-pills'>
          <li>
            <a href='/'>
              <span class='glyphicon glyphicon-home'></span>
              Home
            </a>
          </li>
          <li>
            <a href='/doc/'>
              <span class='glyphicon glyphicon-book'></span>
              Documentation
            </a>
          </li>
          <li>
            <a href='/blog/'>
              <span class='glyphicon glyphicon-bullhorn'></span>
              Blog
            </a>
          </li>
          <li>
            <a href='/contact.html'>
              <span class='glyphicon glyphicon-inbox'></span>
              Contact
            </a>
          </li>
        </ul>
      </div>
      <ol class="breadcrumb"><li><a href="/">Home</a></li><li><a href='../../'>Howl Documentation (master branch)</a></li><li>Spec</li><li>Io</li><li>Spec - howl.io.File</li></ol>
      <div class="spec-group spec-group-1">&#x000A;&#x000A;<h1 id="howl.io.file">howl.io.File</h1>&#x000A;&#x000A;<h4 id=".basename-returns-the-basename-of-the-path">.basename returns the basename of the path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'base.ext'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/foo/base.ext'</span><span class="p">).</span><span class="n">basename</span></pre>&#x000A;&#x000A;<h4 id=".extension-returns-the-extension-of-the-path">.extension returns the extension of the path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/foo/base.ext'</span><span class="p">).</span><span class="n">extension</span><span class="p">,</span><span class="w"> </span><span class="s1">'ext'</span></pre>&#x000A;&#x000A;<h4 id=".path-returns-the-path-of-the-file">.path returns the path of the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'/foo/base.ext'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/foo/base.ext'</span><span class="p">).</span><span class="n">path</span></pre>&#x000A;&#x000A;<h4 id=".uri-returns-an-uri-representing-the-path">.uri returns an URI representing the path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/foo.txt'</span><span class="p">).</span><span class="n">uri</span><span class="p">,</span><span class="w"> </span><span class="s1">'file:///foo.txt'</span></pre>&#x000A;&#x000A;<h4 id=".exists-returns-true-if-the-path-exists">.exists returns true if the path exists</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span></pre>&#x000A;&#x000A;<h4 id=".parent-return-the-parent-of-the-file">.parent return the parent of the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/ls'</span><span class="p">).</span><span class="n">parent</span><span class="p">.</span><span class="n">path</span><span class="p">,</span><span class="w"> </span><span class="s1">'/bin'</span></pre>&#x000A;&#x000A;<h4 id=".children-returns-a-table-of-children">.children returns a table of children</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1'</span><span class="p">)</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child2'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">kids</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dir</span><span class="p">.</span><span class="n">children</span><span class="w">&#x000A;  </span><span class="nb">table.sort</span><span class="w"> </span><span class="n">kids</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">path</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">path</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">[</span><span class="n">v</span><span class="p">.</span><span class="n">basename</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">*</span><span class="n">kids</span><span class="p">],</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'child1'</span><span class="p">,</span><span class="w"> </span><span class="s1">'child2'</span><span class="w"> </span><span class="p">}</span></pre>&#x000A;&#x000A;<h4 id=".file_type-is-a-string-describing-the-file-type">.file_type is a string describing the file type</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'directory'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin'</span><span class="p">).</span><span class="n">file_type</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'regular'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/ls'</span><span class="p">).</span><span class="n">file_type</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'special'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/dev/null'</span><span class="p">).</span><span class="n">file_type</span></pre>&#x000A;&#x000A;<h4 id=".writeable-is-true-if-the-file-represents-a-entry-that-can-be-written-to">.writeable is true if the file represents a entry that can be written to</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">dir</span><span class="p">.</span><span class="n">writeable</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dir</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'file.txt'</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">writeable</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">writeable</span><span class="w">&#x000A;&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/no/such/directory/orfile.txt'</span><span class="p">).</span><span class="n">writeable</span></pre>&#x000A;&#x000A;<h4 id=".readable-is-true-if-the-file-represents-a-entry-that-can-be-read">.readable is true if the file represents a entry that can be read</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">dir</span><span class="p">.</span><span class="n">readable</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dir</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'file.txt'</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">readable</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">readable</span></pre>&#x000A;&#x000A;<h4 id=".etag-is-a-string-that-can-be-used-to-check-for-modification">.etag is a string that can be used to check for modification</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is</span><span class="p">.</span><span class="n">not_nil</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">etag</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nb">type</span><span class="p">(</span><span class="n">file</span><span class="p">.</span><span class="n">etag</span><span class="p">),</span><span class="w"> </span><span class="s1">'string'</span></pre>&#x000A;&#x000A;<h4 id=".modified_at-is-a-the-unix-time-when-the-file-was-last-modified">.modified_at is a the unix time when the file was last modified</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is</span><span class="p">.</span><span class="n">not_nil</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">modified_at</span></pre>&#x000A;&#x000A;<h4 id="read(..)-is-a-short-hand-for-doing-a-read(..)-on-the-lua-file-handle">read(..) is a short hand for doing a read(..) on the Lua file handle</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'first line\n'</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'first'</span><span class="p">,</span><span class="w"> </span><span class="s1">' line'</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">read</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="s1">'*l'</span><span class="w"> </span><span class="p">}</span></pre>&#x000A;&#x000A;<h4 id="join()-returns-a-new-file-representing-the-specified-child">join() returns a new file representing the specified child</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin'</span><span class="p">)</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'ls'</span><span class="p">).</span><span class="n">path</span><span class="p">,</span><span class="w"> </span><span class="s1">'/bin/ls'</span></pre>&#x000A;&#x000A;<h4 id="relative_to_parent()-returns-a-path-relative-to-the-specified-parent">relative_to_parent() returns a path relative to the specified parent</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">parent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/bin'</span><span class="w">&#x000A;</span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'ls'</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">relative_to_parent</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span></pre>&#x000A;&#x000A;<h4 id="is_below(dir)-returns-true-if-the-file-is-located-beneath-&lt;dir&gt;">is_below(dir) returns true if the file is located beneath &lt;dir&gt;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">parent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/bin'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/ls'</span><span class="p">)</span><span class="o">\</span><span class="n">is_below</span><span class="w"> </span><span class="n">parent</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/sub/ls'</span><span class="p">)</span><span class="o">\</span><span class="n">is_below</span><span class="w"> </span><span class="n">parent</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/usr/bin/ls'</span><span class="p">)</span><span class="o">\</span><span class="n">is_below</span><span class="w"> </span><span class="n">parent</span></pre>&#x000A;&#x000A;<h4 id="rm-and-unlink-is-an-alias-for-delete">rm and unlink is an alias for delete</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">rm</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">delete</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">unlink</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">delete</span></pre>&#x000A;&#x000A;<h4 id="rm_r-is-an-alias-for-delete_all">rm_r is an alias for delete_all</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">rm_r</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">delete_all</span></pre>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="tmpfile">tmpfile<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="returns-a-file-instance-pointing-to-an-existing-file">returns a file instance pointing to an existing file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">tmpfile</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span><span class="w">&#x000A;</span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="with_tmpfile">with_tmpfile<span class="arg-list">(f)</span></h2>&#x000A;&#x000A;<h4 id="invokes-&lt;f&gt;-with-the-file">invokes &lt;f&gt; with the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">spy</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s1">'File'</span><span class="p">,</span><span class="w"> </span><span class="n">typeof</span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w">&#x000A;&#x000A;</span><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="n">f</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">spy</span><span class="p">(</span><span class="n">f</span><span class="p">).</span><span class="n">was_called</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span></pre>&#x000A;&#x000A;<h4 id="removes-the-temporary-file-even-if-&lt;f&gt;-raises-an-error">removes the temporary file even if &lt;f&gt; raises an error</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="kd">local</span><span class="w"> </span><span class="n">tmpfile</span><span class="w">&#x000A;</span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">tmpfile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="w">&#x000A;  </span><span class="nb">error</span><span class="w"> </span><span class="s1">'noo'</span><span class="w">&#x000A;&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'noo'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="n">f</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">tmpfile</span><span class="p">.</span><span class="n">exists</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="tmpdir">tmpdir<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="returns-a-file-instance-pointing-to-an-existing-directory">returns a file instance pointing to an existing directory</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">tmpdir</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">is_directory</span><span class="w">&#x000A;</span><span class="n">file</span><span class="o">\</span><span class="n">delete_all</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="expand_path">expand_path<span class="arg-list">(path)</span></h2>&#x000A;&#x000A;<h4 id="expands-&quot;~&quot;-into-the-full-path-of-the-home-directory">expands &quot;~&quot; into the full path of the home directory</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equals</span><span class="w"> </span><span class="s2">"</span><span class="si">#{</span><span class="nb">os.getenv</span><span class="p">(</span><span class="err">'</span><span class="nc">HOME</span><span class="err">'</span><span class="p">)</span><span class="si">}</span><span class="s2">/foo.txt"</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="nc">File</span><span class="p">.</span><span class="n">expand_path</span><span class="w"> </span><span class="s1">'~/foo.txt'</span><span class="p">)</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="new">new<span class="arg-list">(p, cwd)</span></h2>&#x000A;&#x000A;<h4 id="accepts-a-string-as-denothing-a-path">accepts a string as denothing a path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="w"> </span><span class="s1">'/bin/ls'</span></pre>&#x000A;&#x000A;<h4 id="accepts-other-files-as-well">accepts other files as well</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="w">&#x000A;</span><span class="n">f2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="n">f</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="n">f2</span></pre>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when &lt;cwd&gt; is specified)</span></h3>&#x000A;&#x000A;<h4 id="resolves-a-string-&lt;p&gt;-relative-to-&lt;cwd&gt;">resolves a string &lt;p&gt; relative to &lt;cwd&gt;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'ls'</span><span class="p">,</span><span class="w"> </span><span class="s1">'/bin'</span><span class="p">).</span><span class="n">path</span></pre>&#x000A;&#x000A;<h4 id="resolves-an-absolute-string-&lt;p&gt;-as-the-absolute-path">resolves an absolute string &lt;p&gt; as the absolute path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/ls'</span><span class="p">,</span><span class="w"> </span><span class="s1">'/home'</span><span class="p">).</span><span class="n">path</span></pre>&#x000A;&#x000A;<h4 id="accepts-other-files-as-&lt;cwd&gt;">accepts other Files as &lt;cwd&gt;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'ls'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin'</span><span class="p">)).</span><span class="n">path</span></pre>&#x000A;</div>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id=".is_absolute">.is_absolute</h2>&#x000A;&#x000A;<h4 id="returns-true-if-the-given-path-is-absolute">returns true if the given path is absolute</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">is_absolute</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">is_absolute</span><span class="w"> </span><span class="s1">'c:\\\\bin\\ls'</span></pre>&#x000A;&#x000A;<h4 id="returns-false-if-the-given-path-is-absolute">returns false if the given path is absolute</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">is_absolute</span><span class="w"> </span><span class="s1">'bin/ls'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">is_absolute</span><span class="w"> </span><span class="s1">'bin\\ls'</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id=".display_name">.display_name</h2>&#x000A;&#x000A;<h4 id="is-the-same-as-the-basename-for-files">is the same as the basename for files</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'base.ext'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/foo/base.ext'</span><span class="p">).</span><span class="n">display_name</span></pre>&#x000A;&#x000A;<h4 id="has-a-trailing-separator-for-directories">has a trailing separator for directories</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'bin/'</span><span class="p">,</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/usr/bin'</span><span class="p">).</span><span class="n">display_name</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id=".short_path">.short_path</h2>&#x000A;&#x000A;<h4 id="returns-the-path-with-the-home-directory-replace-by-&quot;~&quot;">returns the path with the home directory replace by &quot;~&quot;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="nb">os.getenv</span><span class="p">(</span><span class="s1">'HOME'</span><span class="p">))</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'foo.txt'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'~/foo.txt'</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">short_path</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="contents">contents</h2>&#x000A;&#x000A;<h4 id="assigning-a-string-writes-the-string-to-the-file">assigning a string writes the string to the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'hello world'</span><span class="w">&#x000A;  </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">io.open</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">path</span><span class="w">&#x000A;  </span><span class="n">read_back</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f</span><span class="o">\</span><span class="n">read</span><span class="w"> </span><span class="s1">'*all'</span><span class="w">&#x000A;  </span><span class="n">f</span><span class="o">\</span><span class="n">close</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">read_back</span><span class="p">,</span><span class="w"> </span><span class="s1">'hello world'</span></pre>&#x000A;&#x000A;<h4 id="returns-the-contents-of-the-file">returns the contents of the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">io.open</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">path</span><span class="p">,</span><span class="w"> </span><span class="s1">'w'</span><span class="w">&#x000A;  </span><span class="n">f</span><span class="o">\</span><span class="n">write</span><span class="w"> </span><span class="s1">'hello world'</span><span class="w">&#x000A;  </span><span class="n">f</span><span class="o">\</span><span class="n">close</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="p">,</span><span class="w"> </span><span class="s1">'hello world'</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="open">open<span class="arg-list">([mode, function])</span></h2>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when &lt;function&gt; is nil)</span></h3>&#x000A;&#x000A;<h4 id="returns-a-lua-file-handle">returns a Lua file handle</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'first line\nsecond line\n'</span><span class="w">&#x000A;  </span><span class="n">fh</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">open</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'first line'</span><span class="p">,</span><span class="w"> </span><span class="n">fh</span><span class="o">\</span><span class="n">read</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'second line\n'</span><span class="p">,</span><span class="w"> </span><span class="n">fh</span><span class="o">\</span><span class="n">read</span><span class="w"> </span><span class="s1">'*L'</span><span class="w">&#x000A;  </span><span class="n">fh</span><span class="o">\</span><span class="n">close</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when &lt;function&gt; is provided)</span></h3>&#x000A;&#x000A;<h4 id="it-is-invoked-with-the-file-handle">it is invoked with the file handle</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="p">.</span><span class="n">contents</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'first line\nsecond line\n'</span><span class="w">&#x000A;  </span><span class="kd">local</span><span class="w"> </span><span class="n">first_line</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">open</span><span class="w"> </span><span class="s1">'r'</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">fh</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;    </span><span class="n">first_line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fh</span><span class="o">\</span><span class="n">read</span><span class="o">!</span><span class="w">&#x000A;&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'first line'</span><span class="p">,</span><span class="w"> </span><span class="n">first_line</span></pre>&#x000A;&#x000A;<h4 id="returns-the-returns-values-of-the-function">returns the returns values of the function</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'callback'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="s1">'last'</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">open</span><span class="w"> </span><span class="s1">'r'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="s1">'callback'</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="s1">'last'</span><span class="w"> </span><span class="p">}</span></pre>&#x000A;&#x000A;<h4 id="closes-the-file-automatically-after-invoking-&lt;function&gt;">closes the file automatically after invoking &lt;function&gt;</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="kd">local</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">open</span><span class="w"> </span><span class="s1">'r'</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">fh</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fh</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">has_errors</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">handle</span><span class="o">\</span><span class="n">read</span><span class="o">!</span></pre>&#x000A;<div class="spec-group spec-group-4">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(..  when &lt;function&gt; raises an error)</span></h3>&#x000A;&#x000A;<h4 id="propagates-that-error">propagates that error</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">raises</span><span class="w"> </span><span class="s1">'kaboom'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">open</span><span class="w"> </span><span class="s1">'r'</span><span class="p">,</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nb">error</span><span class="w"> </span><span class="s1">'kaboom'</span></pre>&#x000A;&#x000A;<h4 id="still-closes-the-file">still closes the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="kd">local</span><span class="w"> </span><span class="n">handle</span><span class="w">&#x000A;  </span><span class="nb">pcall</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">open</span><span class="w"> </span><span class="s1">'r'</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">fh</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;    </span><span class="n">handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fh</span><span class="w">&#x000A;    </span><span class="nb">error</span><span class="w"> </span><span class="s1">'kaboom'</span><span class="w">&#x000A;&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">has_errors</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">handle</span><span class="o">\</span><span class="n">read</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;</div>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="mkdir">mkdir<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="creates-a-directory-for-the-path-specified-by-the-file">creates a directory for the path specified by the file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">is_directory</span></pre>&#x000A;&#x000A;<h4 id="raises-an-error-if-the-directory-could-not-be-created">raises an error if the directory could not be created</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">has_error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/aksdjskjdgudfkj'</span><span class="p">)</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="mkdir_p">mkdir_p<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="creates-a-directory-for-the-path-specified-by-the-file,-including-parents">creates a directory for the path specified by the file, including parents</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">join</span><span class="w"> </span><span class="s1">'sub/foo'</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">mkdir_p</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">is_directory</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="delete">delete<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="deletes-the-target-file">deletes the target file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span></pre>&#x000A;&#x000A;<h4 id="raise-an-error-if-the-file-does-not-exist">raise an error if the file does not exist</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">.</span><span class="n">tmpfile</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="delete_all">delete_all<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="raise-an-error-if-the-file-does-not-exist">raise an error if the file does not exist</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span></pre>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(for a regular file)</span></h3>&#x000A;&#x000A;<h4 id="deletes-the-target-file">deletes the target file</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">delete_all</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(for a directory)</span></h3>&#x000A;&#x000A;<h4 id="deletes-the-directory-and-all-sub-entries">deletes the directory and all sub entries</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1'</span><span class="p">)</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1/sub_child'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child2'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">dir</span><span class="o">\</span><span class="n">delete_all</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_false</span><span class="w"> </span><span class="n">dir</span><span class="p">.</span><span class="n">exists</span></pre>&#x000A;</div>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="touch">touch<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="creates-the-file-if-does-not-exist">creates the file if does not exist</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">delete</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">file</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">is_true</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">exists</span></pre>&#x000A;&#x000A;<h4 id="raises-an-error-if-the-file-could-not-be-created">raises an error if the file could not be created</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/no/does/not/exist'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="tostring">tostring<span class="arg-list">()</span></h2>&#x000A;&#x000A;<h4 id="returns-a-string-containing-the-path">returns a string containing the path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="nc">File</span><span class="p">.</span><span class="n">with_tmpfile</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">to_s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="nb">tostring</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="s1">'string'</span><span class="p">,</span><span class="w"> </span><span class="n">typeof</span><span class="w"> </span><span class="n">to_s</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">to_s</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">path</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="find">find<span class="arg-list">()</span></h2>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_populated_dir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">with_tmpdir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;    </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1'</span><span class="p">)</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span><span class="w">&#x000A;    </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1/sub_dir'</span><span class="p">)</span><span class="o">\</span><span class="n">mkdir</span><span class="o">!</span><span class="w">&#x000A;    </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1/sub_dir/deep.lua'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;    </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1/sub_child.txt'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;    </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child1/sandwich.lua'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;    </span><span class="n">dir</span><span class="o">\</span><span class="n">join</span><span class="p">(</span><span class="s1">'child2'</span><span class="p">)</span><span class="o">\</span><span class="n">touch</span><span class="o">!</span><span class="w">&#x000A;    </span><span class="n">f</span><span class="w"> </span><span class="n">dir</span></pre>&#x000A;&#x000A;<h4 id="raises-an-error-if-the-file-is-not-a-directory">raises an error if the file is not a directory</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/no/does/not/exist'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">error</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="n">find</span><span class="o">!</span></pre>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(with no parameters given)</span></h3>&#x000A;&#x000A;<h4 id="returns-a-list-of-all-sub-entries">returns a list of all sub entries</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_populated_dir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dir</span><span class="o">\</span><span class="n">find</span><span class="o">!</span><span class="w">&#x000A;  </span><span class="nb">table.sort</span><span class="w"> </span><span class="n">files</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">path</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">b</span><span class="p">.</span><span class="n">path</span><span class="w">&#x000A;  </span><span class="n">normalized</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">f</span><span class="o">\</span><span class="n">relative_to_parent</span><span class="w"> </span><span class="n">dir</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">*</span><span class="n">files</span><span class="p">]</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w">&#x000A;    </span><span class="s1">'child1'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sandwich.lua'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sub_child.txt'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sub_dir'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sub_dir/deep.lua'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child2'</span><span class="w">&#x000A;  </span><span class="p">},</span><span class="w"> </span><span class="n">normalized</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when the sort parameter is given)</span></h3>&#x000A;&#x000A;<h4 id="returns-a-list-of-all-sub-entries-in-a-pleasing-order">returns a list of all sub entries in a pleasing order</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_populated_dir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dir</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="ss">sort:</span><span class="w"> </span><span class="kc">true</span><span class="w">&#x000A;  </span><span class="n">normalized</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">f</span><span class="o">\</span><span class="n">relative_to_parent</span><span class="w"> </span><span class="n">dir</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">*</span><span class="n">files</span><span class="p">]</span><span class="w">&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="n">normalized</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w">&#x000A;    </span><span class="s1">'child2'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sandwich.lua'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sub_child.txt'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sub_dir'</span><span class="p">,</span><span class="w">&#x000A;    </span><span class="s1">'child1/sub_dir/deep.lua'</span><span class="p">,</span><span class="w">&#x000A;  </span><span class="p">}</span></pre>&#x000A;</div>&#x000A;<div class="spec-group spec-group-3">&#x000A;&#x000A;<h3 id=""><span class="arg-list">(when filter: is passed as an option)</span></h3>&#x000A;&#x000A;<h4 id="excludes-files-for-which-&lt;filter(file)&gt;-returns-true">excludes files for which &lt;filter(file)&gt; returns true</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">with_populated_dir</span><span class="w"> </span><span class="p">(</span><span class="n">dir</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;  </span><span class="n">files</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dir</span><span class="o">\</span><span class="n">find</span><span class="w"> </span><span class="ss">filter:</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="o">-&gt;</span><span class="w">&#x000A;    </span><span class="n">file</span><span class="p">.</span><span class="n">basename</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s1">'sandwich.lua'</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="n">file</span><span class="p">.</span><span class="n">basename</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="s1">'child1'</span><span class="w">&#x000A;&#x000A;  </span><span class="n">assert</span><span class="p">.</span><span class="n">same</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s1">'child1'</span><span class="p">,</span><span class="w"> </span><span class="s1">'sandwich.lua'</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">[</span><span class="n">f</span><span class="p">.</span><span class="n">basename</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">*</span><span class="n">files</span><span class="p">]</span></pre>&#x000A;</div>&#x000A;</div>&#x000A;<div class="spec-group spec-group-2">&#x000A;&#x000A;<h2 id="meta-methods">meta methods</h2>&#x000A;&#x000A;<h4 id="/-and-..-joins-the-file-with-the-specified-argument">/ and .. joins the file with the specified argument</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin'</span><span class="p">)</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="s1">'ls'</span><span class="p">).</span><span class="n">path</span><span class="p">,</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="p">(</span><span class="n">file</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="s1">'ls'</span><span class="p">).</span><span class="n">path</span><span class="p">,</span><span class="w"> </span><span class="s1">'/bin/ls'</span></pre>&#x000A;&#x000A;<h4 id="tostring-returns-the-result-of-file.tostring">tostring returns the result of File.tostring</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">file</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">File</span><span class="w"> </span><span class="s1">'/bin/ls'</span><span class="w">&#x000A;</span><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="n">file</span><span class="o">\</span><span class="nb">tostring</span><span class="o">!</span><span class="p">,</span><span class="w"> </span><span class="nb">tostring</span><span class="w"> </span><span class="n">file</span></pre>&#x000A;&#x000A;<h4 id="==-returns-true-if-the-files-point-to-the-same-path">== returns true if the files point to the same path</h4>&#x000A;&#x000A;<pre class="highlight moonscript"><span class="n">assert</span><span class="p">.</span><span class="n">equal</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/ls'</span><span class="p">),</span><span class="w"> </span><span class="nc">File</span><span class="p">(</span><span class="s1">'/bin/ls'</span><span class="p">)</span></pre>&#x000A;</div>&#x000A;</div>
      <div class='footer text-muted'>
        <a href='/'>
          <img width="50" height="50" class="footer-logo" src="/images/howl.png" />
        </a>
        <div class='footer-follow'>
          <p>
            <a class='twitter-follow-button' data-lang='en' data-show-count='false' href='https://twitter.com/howleditor' rel='me'>
              Follow @howleditor
            </a>
          </p>
          <p>
            <a class='twitter-share-button' data-count='none' data-hashtags='howleditor' data-lang='en' data-text='The Howl Editor, a general purpose, light-weight customizable editor.' data-url='http://howl.io' href='https://twitter.com/share'>
              Tweet
            </a>
          </p>
        </div>
        <div class='footer-blurb'>
          <div>The Howl editor.</div>
          <div>
            Copyright 2012-2016
            <a class='alert-link' href='https://github.com/howl-editor/howl/contributors'>
              The Howl Developers.
            </a>
          </div>
        </div>
      </div>
    </div>
    <script>
      <!-- / GA -->
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-45283282-1', 'howl.io');
      ga('send', 'pageview');
      <!-- / Twitter -->
      !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];
      if(!d.getElementById(id)){js=d.createElement(s);js.id=id;
      js.src="//platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
    </script>
  </body>
</html>
